function CalendarHistoryItem(months, travel, transactionNumber) {
    this._travel = travel;
    this._months = months? this.recordMonths(months) : [];
    this._transactionNumber = transactionNumber;
}
CalendarHistoryItem.prototype = {
    recordMonths: function(months) {
        var toDto = function(item) { return item.toDto(); };
        return months.select(toDto);
    },

    getMonths: function() {
        return this._months;
    },

    getTravel: function() {
        return this._travel;
    },

    getTransactionNumber: function() {
        return this._transactionNumber;
    },

    hasMonth: function() {
        return this._months.length > 0;
    },
    hasTravel: function() {
        return this._travel ? true : false;
    }
};

function CalendarHistory() {
    this._transactionNumber = 0;
    this._historyStack = new Stack();
};

CalendarHistory.prototype = {
    beginTransaction: function() {
        this._transactionNumber += 1;
    },

    add: function(months, travel) {
        this._clearOldTransactions();
        this._historyStack.push(new CalendarHistoryItem(months, travel, this._transactionNumber));
    },

    isEmpty: function() {
        return this._historyStack.isEmpty();
    },

    getLastTransaction: function() {
        var result = [];
        while (!this._historyStack.isEmpty()) {
            result.push(this._historyStack.pop());
        }
        return result;
    },

    _clearOldTransactions: function() {
        while (!this._historyStack.isEmpty() && this._historyStack.peek().getTransactionNumber() != this._transactionNumber) {
            this._historyStack.pop();
        } 
    }
};

